#include <QtCore>
#include <QtSql>
#include <QtGui>

#include <QSettings>

#include "kxfiretest.h"
#include "ui_kxfiretest.h"

kXFireTest::kXFireTest( QWidget *parent )
    : QDialog(parent),
    m_ui(new Ui::kXFireTest)
{
    m_ui->setupUi(this);
}

void kXFireTest::log( QString txt )
{
    QString otxt("");
    otxt.append(txt);

    qDebug() << txt;
    m_ui->textBrowserLog->append( otxt );
}

void kXFireTest::on_btnLoadGamesDefault_clicked()
{
    QSettings xfg("C:\\Program Files (x86)\\Xfire\\xfire_games.ini", QSettings::IniFormat);

    QStringList keys = xfg.allKeys();
    QStringListModel *lmodel = new QStringListModel();
    QStringList groups = xfg.childGroups();
    QStringList gname("");

    QStringListIterator it(groups);
    while (it.hasNext())
    {
        QString t = it.next();

        log( QString( xfg.value( QString( "%1/LongName" ).arg(t)).toString()));
        gname.insert( 0, QString( "%1\t%2" ).arg(t).arg(xfg.value( QString( "%1/LongName" ).arg(t)).toString()));
        gamenames.insert( 0, xfg.value( QString( "%1/LongName" ).arg(t)).toString());
    }

    lmodel->setStringList(gname);
    log( QString( "Total keys: %1\nTotal groups: %2" ).arg( keys.size()).arg(groups.size()));
    m_ui->listView->setModel(lmodel);


}

void kXFireTest::on_btnLoadGamesFromFile_clicked()
{

}

void kXFireTest::on_btnLoadGamesFromDB_clicked()
{
    createGameDB("");
}

void kXFireTest::on_btnClearAll_clicked()
{

}

void kXFireTest::on_btnClearLog_clicked()
{
    m_ui->textBrowserLog->clear();
}

void kXFireTest::on_btnDetectGames_clicked()
{

}

void kXFireTest::on_btnClose_clicked()
{

}

void kXFireTest::createGameDB( QString name )
{
    igames = QSqlDatabase::addDatabase( "QSQLITE", "Games" );
    igames.setDatabaseName("games.kdb");

    if( !igames.open())
    {
        log("Unable to establish a database connection.\n"
            "KON Game Manager needs SQLite support. Please read\n"
            "KON documentation for more information.");
    }
    else
        log( "Connection to database established");

    if( !igames.tables(QSql::Tables).contains("games"))
    {
        log( "Creating new GAMES empty database.");
        createEmptyGameDB();
    }
    else
        log( "GAMES database founded, using it!" );
}

bool kXFireTest::createEmptyGameDB()
{
    QSqlQuery query;

    query.exec("create table games ( id int primary key, name varchar(255), sname varchar(12),"
                                   " xid varchar(10), launcherkey varchar(255), launcherexe varchar(255),"
                                   " detectexe varchar(255), launcherurl varchar(255))");
    log( QString( "Creating table GAMES.\n%1" ).arg( query.lastError().databaseText()));

    query.exec("create table iSystem ( name varchar(255), value varchar(255))");
    log( QString( "Creating table iSystem.\n%1" ).arg( query.lastError().databaseText()));
}

void kXFireTest::on_listView_doubleClicked(QModelIndex index)
{
    QSettings xfg("C:\\Program Files (x86)\\Xfire\\xfire_games.ini", QSettings::IniFormat);

    log( QString( "Item data:\n\t%1\nB:\t%2" ).arg(index.data().toString()).arg( xfg.value(index.data().toString()).toString()));

    QString tgid( index.data().toString());
    QString gid = tgid.split( QRegExp("\\s+") ).at(0);

    lgame = gid;

    xfg.beginGroup(gid);
    QStringList keys = xfg.childKeys();
    QStringListModel *lmodel = new QStringListModel();

    lmodel->setStringList(keys);
    m_ui->listViewDetail->setModel(lmodel);

    QStringListIterator it(keys);
    while (it.hasNext())
    {
        QString t = it.next();
        log( QString( "\t%1\t%2" )
             .arg(t)
             .arg(xfg.value(t).toString())
             );
    }

}

void kXFireTest::on_listViewDetail_clicked(QModelIndex index)
{
    QSettings xfg("C:\\Program Files (x86)\\Xfire\\xfire_games.ini", QSettings::IniFormat);
    QString key( index.data().toString());

    xfg.beginGroup(lgame);
    log( QString( "Key %1 - %2" ).arg(key).arg(xfg.value(key).toString()));
}

bool kXFireTest::loadGamesFromDefault()
{
    on_btnLoadGamesDefault_clicked();
    return true;
}
